package org.openfidias.jdbase.template;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.openfidias.jdbase.lang.Choose;
import org.openfidias.jdbase.view.Console;

/**
 *
 * @author atila
 */
public final class View {

   public static void begin(IController controller, IModel model, String view) throws IOException, Exception {
      String option = "";
      while (true) {
         menu(view);
         System.out.print(Console.bash);
         option = Console.read.nextLine();
         if (Choose.is(".create", option)) {
            controller.create();
         } else if (Choose.isAny(new String[]{"*", ".read_all"}, option)) {
            model.list();
         } else if (Choose.is(".read", option)) {
            controller.read();
         } else if (Choose.is(".delete", option)) {
            controller.delete();
         } else if (Choose.is(".quit", option)) {
            // end program.
            System.exit(0);
         } else if (Choose.is(".clear", option)) {
            Console.clearConsole();
         } else if (Choose.is(".back", option)) {
            // leave view.
            break;
         } else {
            System.out.println("Opção Inválida.");
         }
      }
   }

   public static void menu(String view) {
      StringBuilder builder = new StringBuilder();
      String title = " View ".concat(view).concat(" ");
      builder.append(StringUtils.center(title, 60, " · "));
      builder.append("\n\n");
      builder.append("[.create] Criar ".concat(view).concat("\n"));
      builder.append("[.read] Visualizar ".concat(view).concat("\n"));
      builder.append("[.read_all][*] Visualizar Todos ".concat(view).concat("\n"));
      builder.append("[.delete] Deletar ".concat(view).concat("\n"));
      builder.append("[.quit][.back][.clear]");
      System.out.println(builder.toString());
   }
}
